import { app, I_encodeDecodeConfig } from "mydog";
import { cmd } from "../config/cmd";
import { connector_msg } from "../config/proto/connector";
import { database_msg } from "../config/proto/database";
import { doudizhu_msg } from "../config/proto/doudizhu";
import { gate_msg } from "../config/proto/gate";
import { lobby_msg } from "../config/proto/lobby";
import { room_msg, } from "../config/proto/room";

/** 配置编解码 */
export function getEncodeDecodeFunc(): {
  "msgEncode": (cmdId: number, msg: any) => Buffer,
  "msgDecode": (cmdId: number, msg: Buffer) => any
} {
  return {
    "msgEncode": msgEncode,
    "msgDecode": msgDecode
  }
}

function msgEncode(cmdId: number, data: any): Buffer {
  console.log("<<<---", app.routeConfig[cmdId], JSON.stringify(data));
  return msgCoder[cmdId].s2c?.encode(data).finish() as Buffer;
}

function msgDecode(cmdId: number, msgBuf: Buffer): Buffer {
  let msg = msgCoder[cmdId].c2s?.decode(msgBuf);
  console.log("--->>>", app.routeConfig[cmdId], JSON.stringify(msg));
  return msg;
}

// 编码时直接中转Buffer
let encode_buffer = {
  "encode": function (data: Buffer) {
    return {
      "finish": function () {
        return data;
      }
    }
  }
}

// 解码时直接中转Buffer
let decode_buffer = {
  "decode": function (data: Buffer) {
    return data;
  }
}

interface I_msg_con {
  c2s?: { decode: (msg: Buffer) => any };
  s2c?: { encode: (msg: any) => { finish: () => Uint8Array } };
}

let msgCoder: { [cmd: string]: I_msg_con } = {};
// gate
msgCoder[cmd.gate_main_connect] = { "c2s": gate_msg.c2s_connect, "s2c": gate_msg.s2c_connect };
// connector
msgCoder[cmd.connector_main_login] = { "c2s": connector_msg.c2s_login, "s2c": connector_msg.s2c_login };
msgCoder[cmd.connector_main_logout] = { "c2s": connector_msg.c2s_logout, "s2c": connector_msg.s2c_logout };
msgCoder[cmd.connector_main_register] = { "c2s": connector_msg.c2s_register, "s2c": connector_msg.s2c_register };
// lobby
msgCoder[cmd.lobby_main_match] = { "c2s": lobby_msg.c2s_match, "s2c": lobby_msg.s2c_match };
msgCoder[cmd.lobby_main_unmatch] = { "c2s": lobby_msg.c2s_unmatch };
msgCoder[cmd.lobby_main_sendChatMessage] = { "c2s": lobby_msg.c2s_sendChatMessage };
msgCoder[cmd.lobby_onChat] = { "s2c": lobby_msg.s2c_onChat };
msgCoder[cmd.lobby_onEnter] = { "s2c": lobby_msg.s2c_onEnter };
msgCoder[cmd.lobby_onLeave] = { "s2c": lobby_msg.s2c_onLeave };
// room
msgCoder[cmd.room_main_create] = { "c2s": room_msg.c2s_create, "s2c": room_msg.s2c_create };
msgCoder[cmd.room_main_join] = { "c2s": room_msg.c2s_join, "s2c": room_msg.s2c_join };
msgCoder[cmd.room_main_leave] = { "c2s": room_msg.c2s_leave, "s2c": room_msg.s2c_leave };
msgCoder[cmd.room_main_playerAction] = { "c2s": room_msg.c2s_playerAction };
msgCoder[cmd.room_onPlayerInfo] = { "s2c": room_msg.s2c_onPlayerInfo };
msgCoder[cmd.room_onPlayerLeave] = { "s2c": room_msg.s2c_onPlayerLeave };
msgCoder[cmd.room_onMatch] = { "s2c": room_msg.s2c_onMatch };
// database
msgCoder[cmd.database_main_getUser] = { "c2s": database_msg.c2s_getUser, "s2c": database_msg.s2c_getUser };
msgCoder[cmd.database_main_rename] = { "c2s": database_msg.c2s_rename, "s2c": database_msg.s2c_rename };
msgCoder[cmd.database_main_getDoudizhuInfo] = { "c2s": database_msg.c2s_getDoudizhuInfo, "s2c": database_msg.s2c_getDoudizhuInfo };
// doudizhu
msgCoder[cmd.doudizhu_onGameInit] = { "s2c": doudizhu_msg.s2c_onGameInit };
msgCoder[cmd.doudizhu_onGameStart] = { "s2c": doudizhu_msg.s2c_onGameStart };
msgCoder[cmd.doudizhu_main_qiangdizhu] = { "c2s": doudizhu_msg.c2s_qiangdizhu };
msgCoder[cmd.doudizhu_onQiangdizhu] = { "s2c": doudizhu_msg.s2c_onQiangdizhu };
